package org.osomit.sacct.entity;

import org.json.simple.JSONObject;
import org.json.simple.JSONArray;

import java.util.List;
import java.util.Map;

/**
 * 
 * @author Jian Fang (John.Jian.Fang@gmail.com)
 *
 * Date: Jun 28, 2009
 *
 */
public class Request extends Entity {
    private static final long serialVersionUID = 1235300709464796250L;
    public final static String ENTITY_NAME = "Request";

    //random string padding to the request
    public static final String RND = "rnd";
    private String rnd;

    public static final String COMMAND = "command";
    private String command;

    public static final String REQUEST_CONTEXT = "requestContext";
    private RequestContext requestContext;

    public static final String PARAMETERS = "parameters";
    private List parameters;

    public String getRnd() {
        return rnd;
    }

    public void setRnd(String rnd) {
        this.rnd = rnd;
    }

    public String getCommand() {
        return command;
    }

    public void setCommand(String command) {
        this.command = command;
    }

    public RequestContext getRequestContext() {
        return requestContext;
    }

    public void setRequestContext(RequestContext requestContext) {
        this.requestContext = requestContext;
    }

    public List getParameters() {
        return parameters;
    }

    public void setParameters(List parameters) {
        this.parameters = parameters;
    }

    public JSONObject toJSON() {
        JSONObject obj = new JSONObject();

        obj.put(RND, this.rnd);
        obj.put(COMMAND, this.command);
        obj.put(REQUEST_CONTEXT, ((this.requestContext == null) ? null: this.requestContext.toJSON()));
        JSONArray arr = new JSONArray();
        if(parameters != null && parameters.size() > 0){
            for(Object p: parameters){
                arr.add(p);
            }
        }
        obj.put(PARAMETERS, arr);

        return obj;  
    }
}
